import {Component, signal} from '@angular/core';
import {AuthService} from "../../services/auth.service";
import {FormControl, FormGroup, NonNullableFormBuilder, Validators} from "@angular/forms";

@Component({
  selector: 'app-account',
  templateUrl: './account.component.html',
  styleUrl: './account.component.css'
})
export class AccountComponent {
  constructor(private authService: AuthService,private fb: NonNullableFormBuilder) {
  }
  get isLoggedIn(){
    return this.authService.getIsLoggedIn();
  }
  get Name(){
    return this.authService.getUsername();
  }
  validateForm: FormGroup<{
    userName: FormControl<string>;
    password: FormControl<string>;
    remember: FormControl<boolean>;
  }> = this.fb.group({
    userName: ['', [Validators.required]],
    password: ['', [Validators.required]],
    remember: [true]
  });
  logout(){
    this.authService.logout();
  }

  login(){
    if(this.validateForm.valid){
      const form = this.validateForm.value;
      this.authService.login(form.userName!, form.password!);
    }else{
      Object.values(this.validateForm.controls).forEach(control => {
        if (control.invalid) {
          control.markAsDirty();
          control.updateValueAndValidity({ onlySelf: true });
        }
      });
    }
  }
}
